window.saveVoteGroups=[];
initDatetimepicker('#vote_act_time');
initUploadImageFile('.backgroundImg-container');
initTinymce();
/*分组活动*/
$('#switch-vote-group').bootstrapSwitch();
$('#switch-vote-group').on('switch-change', function (e, data) {
    if(data.value){
       $('.div-vote-group').css('display','block');
    }else {
        window.saveVoteGroups=[];

        $('.div-vote-group').css('display','none');
    }
});
//添加到分组
$('.add-vote-group-butten').click(function () {
    var addObj = $(this).parent('.add-vote-group');
    var preIndex=addObj.prev('.vote-group-form').data('index')
    $.get('/web/activity/vote/group', function (res) {
        addObj.before(res);
        initialVoteGroup(preIndex);
    });
});
$('.vote-group-card').on('click','.vote_group_card_delet',function () {
    if ($(this).closest('.vote-group-card').children('.vote-group-form').length == 1) {
        return;
    }
    $(this).parents('.vote-group-form').remove();
});
function initialVoteGroup(preIndex){
    var obj=$(".vote-group-form[data-index="+preIndex+"]").next();
    var optIndex=preIndex+1;
    obj.attr('data-index',optIndex);
}
//设置选项排序
$( ".vote-group-card" ).sortable({
    items: "li:not(.add-vote-group)",
    cursor: "move"
});
$( ".vote-group-card" ).disableSelection();
//保存分组
$('.save-vote-group-butten').click(function () {
    var flag=0;
    var tempVoteGroups=[];
    $.extend(tempVoteGroups,window.saveVoteGroups);
    window.saveVoteGroups=[];
    $('.vote-group-form').each(function () {
        if(!$(this).find('input').val()){
            flag=1;
            window.saveVoteGroups=tempVoteGroups;
            return false;
        }
        window.saveVoteGroups.push({"index":$(this).data('index'),"value":$(this).find('input').val()});
    })
    if(flag==0){
        utils.showNotification('分组设值保存成功',2);
    }else {
        utils.showNotification('请将分组信息填写完整或删除不需要的分组',3);
    }
});

/*列表配置*/
initUploadImageFile('.list-sm-images');
/*选项设置*/
//点击‘选项设置事件’
$('#voteSet').click(function () {
    $('.groups-tab').remove();
    $('#bootstrap-vote-table-container').before('<ul role="tablist" class="nav nav-tabs groups-tab">'+
                                                    '<li role="presentation"  data-group="all" data-name="" class="active">'+
                                                    '<a href="javascript:;" data-toggle="tab" aria-expanded="true">全部</a>'+
                                                    '</li>'+
                                                  '</ul>');
    alert('fd');
    if(window.saveVoteGroups.length!=0){
        // alert('fgfgf');
        // $('.groups-tab').css('display','none');
        // $('#activity_tab_2').children('.card').css('display','none');
        // $('#activity_tab_2').children('h3:first-child').remove();
        // $('#activity_tab_2').prepend('<h3 class="pull-center">暂无分组</h3>');
    // }else{
        // $('#activity_tab_2').children('h3:first-child').remove();
        // $('.groups-tab').css('display','block');
        alert('f');
        $('.groups-tab').empty();
        $('.groups-tab').append( '<li role="presentation"  data-group="all" data-name="" class="active">'+
            '<a href="javascript:;" data-toggle="tab" aria-expanded="true">全部</a>'+
            '</li>');
        //选项中的分组选项卡设置
        window.saveVoteGroups.forEach(function (val,key) {
            var html='<li role="presentation"  data-group="'+val['index']+'" data-name="'+val['value']+'">'+
                '<a href="javascript:;" data-toggle="tab" aria-expanded="true">'+val['value']+'</a>'+
                '</li>';
            $('.groups-tab').append(html);
        });
        // $('.groups-tab li:first-child').addClass('active');
        // $('#activity_tab_2').children('.card').css('display','block');
    }
});
//点击选项分组选项卡事件
$('.groups-tab li').on('click',function () {
    var groupname=$(this).data('name');
    var groupIndex=$(this).data('index');
    $(this).parent().find('li').removeClass('active');
    $(this).addClass('active');
    // $('#bootstrap-table-container').bootstrapTable('refreshOptions',{
    //     queryParams:function(params){
    //         return $.extend(params,{'category':value});
    //     }
    // })
});
var vote_table_option={
    toolbar: ".toolbar",
    search:true,
    searchAlign: 'left',
    buttonsAlign:'right',
    toolbarAlign:'right',
    locale:'zh-CN',
    idField:'id',
    icons: {
        refresh: 'fa fa-refresh',
        toggle: 'fa fa-th-list',
        columns: 'fa fa-columns',
        detailOpen: 'fa fa-plus-circle',
        detailClose: 'fa fa-minus-circle',
    },
    sidePagination:'client',
    data:[{
        "id": 0,
        "smpicture":"/images/admin/trigger_icon2.png",
        "name": "Item 0",
        "isCheck": 1,
        "category":"test1",
        "displayCategory":"video",
        "vidosrc":"/admin/videotest.mp4",
        "describe":"test",
        "introduction":"jkj8io"
    },{
        "id": 1,
        "smpicture":"/images/admin/did.png",
        "name": "Item 1",
        "isCheck": 0,
        "category":"test1",
        "displayCategory":"video",
        "vidosrc":"/admin/videotest.mp4",
        "describe":"test",
        "introduction":"676876879"
    },{
        "id": 2,
        "smpicture":"/images/admin/did.png",
        "name": "Item 2",
        "isCheck": 1,
        "category":"test1",
        "displayCategory":"audio",
        "audiosrc":"/admin/audio.mp3",
        "describe":"test",
        "introduction":"1233435"
    },{
        "id": 3,
        "smpicture":"/images/admin/did.png",
        "name": "Item 3",
        "isCheck": 0,
        "category":"test1",
        "displayCategory":"audio",
        "audiosrc":"/admin/audiotest.mp3",
        "describe":"test",
        "introduction":"676868i"
    }
    ]
};
// var vote_table_options = $.extend({},default_table_option,vote_table_option);
$('#bootstrap-vote-table-container').bootstrapTable(vote_table_option);
$('#bootstrap-vote-table-container').on('post-body.bs.table',function () {
    $('[rel="tooltip"]').tooltip();
});
function imgFormatter(value,row) {
    var str='';
    if(row.smpicture!=''){
        str='<img src="'+row.smpicture+'" class="img-responsive"/>'
    }
    return str;
}
function statusFormatter(value,row) {
    var str = '<span class="text-warning">未通过</span>';
    if (row.isCheck == 1){
        str = '<span class="text-warning">通过</span>';
        return str;
    }
    return str;
}
function operateFormatter(value,row,index){
    var dropdown_id = 'dropdown_'+new Date().getTime();
    var actions = [];
    if(row.isCheck == 1){
        actions.push('<a rel="tooltip" title="取消审核" class="btn btn-simple btn-info btn-icon table-action vote-check" href="javascript:void(0)"><i class="fa fa-check"></i></a>');
    }else{
        actions.push('<a rel="tooltip" title="审核" class="btn btn-simple btn-info btn-icon table-action vote-check" href="javascript:void(0)"><i class="fa fa-ban"></i></a>');
    }
    actions.push( [
        '<a rel="tooltip" title="编辑" class="btn btn-simple btn-warning btn-icon table-action edit" href="/web/activity/vote/edit?id='+row.id+'">',
        '<i class="fa fa-edit"></i>',
        '</a>',
        '<a rel="tooltip" title="查看" class="btn btn-simple btn-warning btn-icon table-action vote-view" href="javascript:void(0)">',
        '<i class="fa fa-eye"></i>',
        '</a>',
        '<a rel="tooltip" title="投票记录" class="btn btn-simple btn-warning btn-icon table-action vote-recode" href="javascript:void(0)">',
        '<i class="fa fa-area-chart"></i>',
        '</a>',
    ].join(''));
    return actions.join('');
}

// $('.vote-check').click(function () {
//     alert('click check');
// });
window.operateEvents={
    'click .vote-check':function (e,value,row,index) {
        var rowid=row.id;
        var isCheck = row.isCheck == 1?0:1;
        // $.post('',{id:row.id,isCheck:isCheck},function (res) {
        //     if (res.code == 0){
        //         utils.showNotification('操作成功',2);
        //         $('#bootstrap-vote-table-container').bootstrapTable('refresh');
        //     }else {
        //         utils.showNotification('操作失败',3);
        //     }
        // })
        row.isCheck=isCheck;
        $('#bootstrap-vote-table-container').bootstrapTable('updateRow',{index: index, row: row});
    },
    'click .vote-view':function (e,value,row) {
        var viewModal=utils.modal('查看选项', function (ele) {
            var optionName=row.name;
            var optionCategory=row.category;
            var displayCategory=row.displayCategory;
            var imgSrc=row.smpicture;
            var display='',displayTitle='展示内容';
            if(displayCategory=='video'){
                display='<video width="320" height="240" controls="controls"><source src="'+row.vidosrc+'" type="video/mp4"></video>';
            }else if(displayCategory=='audio'){
                display='<audio controls="controls"><source src="'+row.audiosrc+'" type="audio/mpeg"></audio>';
            }else{
                displayTitle='';
            }
            var describe=row.describe;
            var introduction=row.introduction;
            var html='<div class="row">'+
                       '<div class="col-md-12 col-sm-12">'+
                            '<form class="form-horizontal">'+
                            '<div class="form-group">'+
                                '<label class="col-md-2 control-label">名称</label>'+
                                '<div class="col-md-7 control-label" style="text-align: left">'+
                                    optionName+
                                '</div>'+
                            '</div>'+
                            '<div class="form-group">'+
                                '<label class="col-md-2 control-label">组别</label>'+
                                '<div class="col-md-7 control-label" style="text-align: left">'+
                                    optionCategory+
                                '</div>'+
                            '</div>'+
                            '<div class="form-group">'+
                                '<label class="col-md-2 control-label">缩略图</label>'+
                                '<div class="col-md-7" >'+
                                    '<div class="vote-view-smimg">'+
                                    '<img src="'+imgSrc+'" class="img-responsive">'+
                                    '</div>'+
                                '</div>'+
                            '</div>'+
                            '<div class="form-group">'+
                                '<label class="col-md-2 control-label">'+displayTitle+'</label>'+
                                '<div class="col-md-7" >'+
                                     display+
                                '</div>'+
                            '</div>'+
                            '<div class="form-group">'+
                                '<label class="col-md-2 control-label">描述</label>'+
                                '<div class="col-md-7 control-label" style="text-align: left">'+
                                    describe+
                                '</div>'+
                            '</div>'+
                            '<div class="form-group">'+
                                '<label class="col-md-2 control-label">详细介绍</label>'+
                                '<div class="col-md-7 control-label" style="text-align: left">'+
                                     introduction+
                                '</div>'+
                            '</div>'+
                            '</form>'+
                       '</div>'+
                    '</div>';
            ele.find('.modal-body').html(html);
        },['返回'], function (res) {
            if (res == 1) {
                viewModal.modal('hide');
            }
        })
    },
    'click .vote-recode':function (e,value,row) {
        var optionName=row.name;
        var recodeModal=utils.modal(optionName+'投票记录', function (ele) {
            var category = $('input[name="category"]').val();
            var html = '<table class="table" id="voteRecodeTable" adta-url=""><thead><tr><th data-field="name">名称</th><th data-field="vote_ip">投票IP</th><th data-field="vote_time">投票时间</th></tr></thead></table>';
            ele.find('.modal-body').html(html);
            var table_option = {
                pagination:false,
                showRefresh:false,
                search: false,
                data:[{
                    "id": 0,
                    "name": "Item 0",
                    "vote_ip": 145,
                    "vote_time":"2017-02-24"
                }, {
                    "id": 1,
                    "name": "Item 1",
                    "vote_ip": 7687,
                    "vote_time":"2017-03-24"
                }
                ],
                queryParams: function (query) {
                    // query['category'] = category;
                    // query['exclude'] = exclude;
                    return query;
                },
                url: ''
            }
            // var recode_table_option = $.extend({},default_table_option, table_option);
            $('#voteRecodeTable').bootstrapTable(table_option);
        }, ['返回'], function (res) {
            if (res == 1) {
                recodeModal.modal('hide');
            }
        })
    }
}
//批量添加
var $table=$('#bootstrap-vote-table-container');
var $allCheck=$('#allCheck');
var selections=[];
$table.on('check.bs.table uncheck.bs.table ' +
    'check-all.bs.table uncheck-all.bs.table', function () {
    $allCheck.prop('disabled', !$table.bootstrapTable('getSelections').length);
    // save your data, here just save the current page
    selections = getIdSelections();
    // push or splice the selections if you want to save all data selections
});
$allCheck.click(function () {
    var ids = getIdSelections();
    console.log($table.bootstrapTable('getSelections'));
    // $.post('',{ids:ids},function (res) {
    //     if (res.code == 0){
    //         utils.showNotification('操作成功',2);
    //         $('#bootstrap-vote-table-container').bootstrapTable('refresh');
    //     }else {
    //         utils.showNotification('操作失败',3);
    //     }
    // })
    $.each($table.bootstrapTable('getSelections'),function (key,row) {
        var rowid=row.id;
        row.isCheck=1;
        $('#bootstrap-vote-table-container').bootstrapTable('updateByUniqueId',{id: rowid, row: row});
    });
    $allCheck.prop('disabled', true);
});
function getIdSelections() {
    return $.map($table.bootstrapTable('getSelections'), function (row) {
        return row.id
    });
}
//添加选项
$('.add-vote-btn').click(function () {
    var url = $(this).attr('data-url');
    // window.location.href = url;
    var addVoteModal=utils.modal('添加选项', function (ele) {
        var html='<div class="row">'+
            '<div class="col-md-12 col-sm-12">'+
                '<form id="addVoteForm" class="form-horizontal" method="" action="">'+
                    '<div class="form-group">'+
                        '<label class="col-md-2 control-label">名称</label>'+
                        '<div class="col-md-7" >'+
                            '<input class="form-control addvote-name" type="text" placeholder="不超过10个字符"  data-msg="名称不得超过10个字符"/>'+
                        '</div>'+
                    '</div>'+
                    '<div class="form-group">'+
                        '<label class="col-md-2 control-label">组别</label>'+
                        '<div class="col-md-7" >'+
                            '<select class="form-control addvote-group">';
        if(window.saveVoteGroups.length==0){
            html=html+'<option selected>暂未添加分组</option>';
        }else {
            $.each(window.saveVoteGroups,function (index,v) {
                html=html+'<option>'+v.value+'</option>';
            });
        }
                     html=html+'</select>'+
                        '</div>'+
                    '</div>'+
                    '<div class="form-group">'+
                        '<label class="col-md-2 control-label">缩略图</label>'+
                        '<div class="col-md-7 vote-upload-smimg" >'+
                            '<input type="file" name="" accept="image/gif,image/jpeg,image/png" data-target="uploadImageFile" data-for="backgroundImg" data-preview-file-type="text" class="form-control" value="" title="">'+
                             '<input type="hidden" name="backgroundImg"  data-preview="" value="">'+
                            '<span class="help-block">*选择展示类型为照片时，使用选项缩略图。</span>'+
                        '</div>'+
                    '</div>'+
                    '<div class="form-group">'+
                        '<label class="col-md-2 control-label">展示类型</label>'+
                        '<div class="col-md-7" >'+
                            '<label class="radio-inline">'+
                                '<input type="radio" name="vote-display-type" class="vote-display-type-pictuer" value="picture" checked> 照片'+
                            '</label>'+
                            '<label class="radio-inline">'+
                                '<input type="radio" name="vote-display-type" class="vote-display-type-video" value="video"> 视屏'+
                            '</label>'+
                            '<label class="radio-inline">'+
                                '<input type="radio" name="vote-display-type" class="vote-display-type-audio" value="audio"> 音频'+
                            '</label>'+
                        '</div>'+
                    '</div>'+
                    '<div class="form-group media-video" style="display: none">'+
                        '<label class="col-md-2 control-label">视频素材</label>'+
                        '<div class="col-md-7" >'+
                            '<input type="file" name="" accept="video/mp4" data-target="uploadMediaFile" data-for="uploadVideo" data-preview-file-type="text" class="form-control" value="" title="">'+
                            '<input type="hidden" name="uploadVideo"  data-preview="" value="">'+
                        '</div>'+
                    '</div>'+
                    '<div class="form-group media-audio" style="display: none">'+
                        '<label class="col-md-2 control-label">音频素材</label>'+
                        '<div class="col-md-7" >'+
                            '<input type="file" name="" accept="audio/mp3" data-target="uploadMediaFile" data-for="uploadAudio" data-preview-file-type="text" class="form-control" value="" title="">'+
                            '<input type="hidden" name="uploadAudio"  data-preview="" value="">'+
                        '</div>'+
                    '</div>'+
                    '<div class="form-group">'+
                        '<label class="col-md-2 control-label">描述</label>'+
                        '<div class="col-md-7" >'+
                            '<input class="form-control vote-desc" type="text" placeholder="不超过20个字符"  data-msg="不得超过20个字符"/>'+
                        '</div>'+
                    '</div>'+
                    '<div class="form-group">'+
                        '<label class="col-md-2 control-label">详细介绍</label>'+
                        '<div class="col-md-7" >'+
                                '<textarea class="form-control tinymceEditor vote-intd"'+
                                   'name="description" placeholder="不超过1000个字" maxlength="1000"'+
                                '></textarea>'+
                        '</div>'+
                    '</div>'+
                '</form>'+
            '</div>'+
            '</div>';
        ele.find('.modal-body').html(html);
    },false, function (res) {
        if (res == 1) {
            var _form=$('#addVoteForm');
            var voteName=_form.find('.addvote-name').val();
            var voteGroup=_form.find('.addvote-group:selected').val();
            var voteSmimg=_form.find('input[name="backgroundImg"]').val();
            var voteDisplayType=_form.find('input[type="radio"]:checked').val();
            var voteDisc=_form.find('.vote-desc').val();
            var voteIntd=_form.find('.vote-intr').val();
            if(voteName==''){
                utils.showNotification('选项名称不能为空',3);
                return false;
            }else if(voteName.length>10){
                utils.showNotification('选项名称最多为10个字符',3);
                return false;
            }else if(voteGroup==''){
                utils.showNotification('请添加分组',3);
                return false;
            }else if(voteSmimg==''){
                utils.showNotification('请添加缩略图',3);
                return false;
            }else if(voteDisplayType=='video'&&!_form.find('input[name="uploadVideo"]').val()){
                utils.showNotification('请添加视频',3);
                return false;
            }else if(voteDisplayType=='audio'&&!_form.find('input[name="uploadAudio"]').val()){
                utils.showNotification('请添加音频',3);
                return false;
            }else if(voteDisc==''){
                utils.showNotification('描述不能为空',3);
                return false;
            }else if(voteDisc.length>20){
                utils.showNotification('描述内容不能超过20个字符',3);
                return false;
            }else if(voteIntd==''){
                utils.showNotification('请添加详细介绍',3);
                return false;
            }else if(voteIntd.length>1000){
                utils.showNotification('详细介绍不能超过1000个字符',3);
                return false;
            }
            var data = _form.serialize();
            var voteaction = _form.attr('action');
            $.post(action,data,function () {
                addVoteModal.modal('hide');
                $('#bootstrap-vote-table-container').bootstrapTable('refresh');
                // if("{{$action}}" == "new_point"){
                //     setTimeout(window.location.href='/web/activityDelivery/show',6000);
                // }
            });
        }
    })
    initialAddVote();
});
//初始化添加选项摸态框内容
function initialAddVote(){
    initUploadImageFile('.vote-upload-smimg');
    $('.vote-display-type-pictuer').click(function () {
        $('.media-video').css('display','none');
        $('.media-audio').css('display','none');
    });
    $('.vote-display-type-video').click(function () {
        $('.media-video').css('display','block');
        $('.media-audio').css('display','none');
        initUploadMediaFile('.media-video','mp4');
    });
    $('.vote-display-type-audio').click(function () {
        $('.media-video').css('display','none');
        $('.media-audio').css('display','block');
        initUploadMediaFile('.media-audio','mp3');
    });
}